package ooday04;

import com.sun.xml.internal.bind.v2.model.core.ID;

//向上造型(多态)的演示
public class Test {
    public static void main(String[] args) {
        //强转成功的条件:
        //1.引用所指向的对象,就是该类对象
        //2.引用指向的对象,实现了该接口或继承了该类型
        /*
        Animal o=new Dog("黑",2,"小黑");
        if (o instanceof Dog){
            Dog g=(Dog)o;
        }
        if (o instanceof Swim){
            Swim a=(Swim) o;
        }
        if (o instanceof Fish){
            Fish b=(Fish) o;
        }
        System.out.println(o instanceof Dog);//t
        System.out.println(o instanceof Swim);//t
        System.out.println(o instanceof Fish);//f

         */

        /*
        Master master=new Master();
        Dog dog=new Dog("黑",3,"小黑");
        Chick chick=new Chick("黄",2,"小黄");
        Fish fish=new Fish("红",3,"小红");
        master.feed(dog);
        master.feed(chick);
        master.feed(fish);

         */

        Animal[] animals=new Animal[5];//动物数组
        animals[0]=new Dog("黑",1,"蔡徐坤");//向上造型,超类型的引用指向派生类的对象
        animals[1]=new Dog("红",2,"小红");
        animals[2]=new Fish("蓝",4,"小蓝");
        animals[3]=new Fish("绿",6,"小绿");
        animals[4]=new Chick("黄",6,"小黄");
        for(int i=0;i<animals.length;i++){//遍历所有动物
            System.out.println(animals[i].name);//输出每个动物的名字
            animals[i].eat();//输出每个动物在吃饭
            animals[i].drin();
            System.out.println();
            if(animals[i] instanceof Dog){//向下则型,强制类型转换
                Dog dog=(Dog) animals[i];
                dog.look();
            }
            if (animals[i] instanceof Swim){
               Swim s=(Swim) animals[i];
                s.swim();
            }
            if (animals[i] instanceof Chick){
               Chick chick=(Chick) animals[i];
               chick.layEss();
            }

        }

    }
}

